package cn.colat.micse.user.controller;


import cn.colat.micse.common.pojo.vo.ResBody;
import cn.colat.micse.user.pojo.dto.UserInfoDTO;
import cn.colat.micse.user.pojo.vo.AccountLoginVO;
import cn.colat.micse.user.pojo.vo.AccountPhoneLoginVO;
import cn.colat.micse.user.pojo.vo.SendSmsVO;
import cn.colat.micse.user.service.user.UserService;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("/account")
@Api(tags = {"用户账号接口"})
public class AccountController {
    private UserService userService;

    @ApiOperation("账号登录")
    @PostMapping("/account-login")
    public ResBody<SaTokenInfo> login(@RequestBody AccountLoginVO vo) {
        SaTokenInfo info = userService.accountLogin(vo);
        return ResBody.success(info);
    }


    @SaCheckLogin
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public ResBody<SaTokenInfo> logout() {
        StpUtil.logout();
        return ResBody.successMessage("退出登录成功");
    }

    @SaCheckLogin
    @ApiOperation("账户信息")
    @GetMapping("/user-info")
    public ResBody<UserInfoDTO> userInfo() {
        UserInfoDTO dto = userService.userInfo();
        return ResBody.success(dto);
    }

    @SaCheckLogin
    @ApiOperation("发送手机验证码")
    @PostMapping("/send-sms-code")
    public ResBody<UserInfoDTO> sendSms(@RequestBody SendSmsVO vo) {
        userService.sendSms(vo);
        return ResBody.successMessage("验证码发送成功");
    }

    @SaCheckLogin
    @ApiOperation("手机号登录")
    @PostMapping("/phone-login")
    public ResBody<SaTokenInfo> phoneLogin(@RequestBody AccountPhoneLoginVO vo) {
        SaTokenInfo dto = userService.phoneLogin(vo);
        return ResBody.success(dto);
    }

}
